/**
 * API类型定义
 * 统一管理所有API接口的请求和响应类型
 */

// =============================================================================
// 基础类型定义
// =============================================================================

/**
 * 统一的API响应结构
 */
export interface ApiResponse<T = any> {
  code: number;
  msg: string;
  data: T;
  timestamp?: string;
}

/**
 * 分页查询参数
 */
export interface PaginationParams {
  page?: number;
  size?: number;
  pageSize?: number;
}

/**
 * 排序参数
 */
export interface SortParams {
  sortBy?: string;
  sortOrder?: "asc" | "desc";
}

/**
 * 通用查询参数
 */
export interface CommonQueryParams extends PaginationParams, SortParams {
  keyword?: string;
  startTime?: string;
  endTime?: string;
}

// =============================================================================
// 用户相关类型
// =============================================================================

/**
 * 用户角色
 */
export type UserRole = "admin" | "teacher" | "student";

/**
 * 用户基础信息
 */
export interface UserInfo {
  id: string;
  name: string;
  phone: string;
  email?: string;
  avatar?: string;
  type: UserRole;
  createTime?: string;
  updateTime?: string;
  // 教师专有字段
  department?: string;
  title?: string;
  subject?: string;
  // 学生专有字段
  studentId?: string;
  major?: string;
  className?: string;
  grade?: string;
}

/**
 * 登录请求
 */
export interface LoginRequest {
  phone: string;
  password: string;
  type: UserRole;
  captcha?: string;
}

/**
 * 登录响应
 */
export interface LoginResponse {
  token: string;
  userInfo: UserInfo;
  expiresIn?: number;
}

/**
 * 注册请求
 */
export interface RegisterRequest {
  phone: string;
  password: string;
  name: string;
  code: string;
  type: UserRole;
  email?: string;
  headImg?: string;
  // 教师字段
  department?: string;
  title?: string;
  subject?: string;
  // 学生字段
  studentId?: string;
  major?: string;
  className?: string;
  grade?: string;
}

/**
 * 发送验证码请求
 */
export interface SendCodeRequest {
  phone: string;
  captcha: string;
}

// =============================================================================
// 班级相关类型
// =============================================================================

/**
 * 班级信息
 */
export interface ClassInfo {
  id: number;
  name: string;
  description?: string;
  teacherId: string;
  teacherName: string;
  capacity?: number;
  enrolled?: number;
  status: "active" | "inactive";
  createTime: string;
  updateTime?: string;
}

/**
 * 创建班级请求
 */
export interface CreateClassRequest {
  name: string;
  description?: string;
  capacity?: number;
}

/**
 * 班级成员信息
 */
export interface ClassMember {
  id: string;
  name: string;
  phone: string;
  studentId?: string;
  major?: string;
  className?: string;
  joinTime: string;
  status: "pending" | "active" | "inactive";
}

// =============================================================================
// 文件相关类型
// =============================================================================

/**
 * 文件信息
 */
export interface FileInfo {
  id: number;
  name: string;
  originalName: string;
  url: string;
  size: number;
  type: string;
  classId?: number;
  uploaderId: string;
  uploaderName: string;
  uploadTime: string;
  isPublished: boolean;
  description?: string;
}

/**
 * 添加文件请求
 */
export interface AddFileRequest {
  name: string;
  url: string;
  classId: number;
  description?: string;
}

/**
 * 文件上传响应
 */
export interface UploadResponse {
  url: string;
  originalName: string;
  size: number;
  type: string;
}

// =============================================================================
// 建议反馈类型
// =============================================================================

/**
 * 建议信息
 */
export interface AdviceInfo {
  id: number;
  content: string;
  classId: number;
  className?: string;
  studentId: string;
  studentName: string;
  createTime: string;
  feedback?: string;
  feedbackTime?: string;
  status: "pending" | "replied";
}

/**
 * 添加建议请求
 */
export interface AddAdviceRequest {
  content: string;
  classId: number;
}

/**
 * 反馈建议请求
 */
export interface FeedbackAdviceRequest {
  adviceId: number;
  feedback: string;
}

// =============================================================================
// 考试相关类型
// =============================================================================

/**
 * 考试信息
 */
export interface ExamInfo {
  id: number;
  name: string;
  description?: string;
  classId: number;
  className?: string;
  startTime: string;
  endTime: string;
  duration: number; // 分钟
  totalScore: number;
  status: "draft" | "published" | "ongoing" | "completed";
  createTime: string;
  questions?: QuestionInfo[];
}

/**
 * 添加考试请求
 */
export interface AddExamRequest {
  name: string;
  description?: string;
  classId: number;
  startTime: string;
  endTime: string;
  duration: number;
  totalScore: number;
}

/**
 * 题目信息
 */
export interface QuestionInfo {
  id: number;
  type: "single" | "multiple" | "essay" | "true_false";
  title: string;
  content: string;
  options?: string[];
  answer?: string | string[];
  score: number;
  analysis?: string;
}

// =============================================================================
// 话题讨论类型
// =============================================================================

/**
 * 话题信息
 */
export interface TopicInfo {
  id: number;
  title: string;
  content: string;
  classId: number;
  className?: string;
  authorId: string;
  authorName: string;
  createTime: string;
  likes: number;
  isLiked: boolean;
  commentCount: number;
  status: "active" | "hidden";
}

/**
 * 添加话题请求
 */
export interface AddTopicRequest {
  title: string;
  content: string;
  classId: number;
}

/**
 * 评论信息
 */
export interface CommentInfo {
  id: number;
  content: string;
  topicId: number;
  authorId: string;
  authorName: string;
  createTime: string;
  replyTo?: {
    id: string;
    name: string;
  };
}

/**
 * 添加评论请求
 */
export interface AddCommentRequest {
  content: string;
  topicId: number;
  replyToId?: string;
}

// =============================================================================
// 统计数据类型
// =============================================================================

/**
 * 仪表板统计数据
 */
export interface DashboardStats {
  totalClasses: number;
  totalStudents: number;
  totalExams: number;
  totalFiles: number;
  recentActivities: ActivityInfo[];
}

/**
 * 活动信息
 */
export interface ActivityInfo {
  id: string;
  type: "class_created" | "exam_added" | "file_uploaded" | "student_joined";
  title: string;
  description: string;
  time: string;
  userId: string;
  userName: string;
}

// =============================================================================
// AI相关类型
// =============================================================================

/**
 * AI聊天请求
 */
export interface ChatRequest {
  message: string;
  conversationId?: string;
}

/**
 * AI聊天响应
 */
export interface ChatResponse {
  message: string;
  conversationId: string;
  timestamp: string;
}

/**
 * AI生成题目请求
 */
export interface GenerateQuestionsRequest {
  subject: string;
  difficulty: "easy" | "medium" | "hard";
  questionCount: number;
  questionType: "single" | "multiple" | "essay" | "mixed";
  topic?: string;
}

// =============================================================================
// 系统配置类型
// =============================================================================

/**
 * 系统设置
 */
export interface SystemSettings {
  siteName: string;
  siteDescription?: string;
  maxFileSize: number; // MB
  allowedFileTypes: string[];
  maintenanceMode: boolean;
  registrationEnabled: boolean;
  emailNotifications: boolean;
}

// =============================================================================
// 健康检查类型
// =============================================================================

/**
 * 健康检查响应
 */
export interface HealthCheckResponse {
  status: "healthy" | "unhealthy";
  timestamp: string;
  version?: string;
  uptime?: number;
  services?: {
    database: "healthy" | "unhealthy";
    redis?: "healthy" | "unhealthy";
    ai?: "healthy" | "unhealthy";
  };
}

// =============================================================================
// 错误类型
// =============================================================================

/**
 * API错误响应
 */
export interface ApiError {
  code: number;
  msg: string;
  details?: any;
  timestamp: string;
  path?: string;
}

/**
 * 表单验证错误
 */
export interface ValidationError {
  field: string;
  message: string;
  value?: any;
}

// =============================================================================
// 导出所有类型
// =============================================================================

export * from "./index";
export * from "./admin";
export * from "./discussion";
